﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing.Common;
using ZXing.QrCode;
using ZXing;
using GK.WCS.Open;

namespace Print.printServer {
    public class QR75CodePrinter {
        String d = "";
        public void print(String Code) {
            d = Code;
           PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printEventHandler);

            pd.DefaultPageSettings.PrinterSettings.PrinterName = GKtsc.PrinterName;       //打印机名称
                                                                                                     //pd.DefaultPageSettings.Landscape = true;  //设置横向打印，不设置默认是纵向的
            pd.PrintController = new StandardPrintController();
            pd.Print();
        }

        private void printEventHandler(object sender, PrintPageEventArgs e) {
               
            Font fntTxt = new Font("宋体", 30, FontStyle.Regular);//正文文字         
                 
            Brush brush = new SolidBrush(Color.Black);//画刷           
             

            try {
                Bitmap bitmap = CreateQRCode(d + ""); 
                e.Graphics.DrawImage(bitmap, 3  , 3 );
                e.Graphics.DrawString(d + "", fntTxt, brush, new Point(100, 250));
            } catch (Exception ee) {
                MessageBox.Show(ee.Message);
            }
        }


   


        /// <summary>
        /// 二维码方法
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public static Bitmap CreateQRCode(string asset) {
            EncodingOptions options = new QrCodeEncodingOptions {
                DisableECI = true,
                CharacterSet = "UTF-8", //编码
                Width = 320,             //宽度
                Height = 320             //高度
            };
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            Bitmap old= writer.Write(asset);

          
            Rectangle cropRect = new Rectangle(20, 40, 320, 320);
            Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
            using (Graphics g = Graphics.FromImage(target)) {
                g.DrawImage(old, new Rectangle(0, 0, target.Width, target.Height),  cropRect,   GraphicsUnit.Pixel);
            }
            return target;
        }
    }
}
